package com.lynx.controller;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lynx.controller.model.AndroidPlugin;
import com.lynx.controller.util.PluginUtil;
import com.lynx.core.BaseController;
import com.lynx.service.PluginStoreService;
import com.lynx.service.model.AndroidPluginBO;

/**
 * @author chris.liu
 * @version 5/6/14 6:12 PM
 */
@Controller("pluginController")
@RequestMapping(value = "/plugin", produces = "text/plain;charset=UTF-8")
public class PluginController extends BaseController {

	@Autowired
	private PluginStoreService pluginStoreService;

	@RequestMapping(value = "/config", method = RequestMethod.GET)
	public ModelAndView androidPluginStore(
			@RequestParam(value = "pageNo", required = false) Integer pageNo) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("plugin_config");
		if (pageNo == null) {
			pageNo = 1;
		}

		try {
			List<AndroidPluginBO> bos = pluginStoreService.getAndroidPlugins(pageNo);
			List<AndroidPlugin> vos = new LinkedList<AndroidPlugin>();
			for (AndroidPluginBO bo : bos) {
				vos.add(PluginUtil.bo2vo(bo));
			}
			modelAndView.addObject("plugins", vos);
		} catch (Exception e) {

		}

		int pageSize = pluginStoreService.getAndroidPluginPageSize();
		modelAndView.addObject("pageSize", pageSize);
		modelAndView.addObject("pageNo", pageNo);
		return modelAndView;
	}

}
